#ifndef _SRC_NET_POLLER_H_
#define _SRC_NET_POLLER_H_

#include <map>
#include <vector>

#include "src/base/time_stamp.h"
#include "src/base/utils.h"
#include "src/net/event_loop.h"

namespace fio66::net {
class Channel;

///
/// Base class for IO Multiplexing
///
/// This class doesn't own the Channel objects.
class Poller {
 public:
  DISALLOW_COPY_AND_ASSIGN(Poller)
  typedef std::vector<Channel*> ChannelList;

  Poller(EventLoop* loop);
  virtual ~Poller();

  /// Polls the I/O events.
  /// Must be called in the loop thread.
  virtual Timestamp Poll(int timeoutMs, ChannelList* activeChannels) = 0;

  /// Changes the interested I/O events.
  /// Must be called in the loop thread.
  virtual void UpdateChannel(Channel* channel) = 0;

  /// Remove the channel, when it destructs.
  /// Must be called in the loop thread.
  virtual void RemoveChannel(Channel* channel) = 0;

  virtual bool HasChannel(Channel* channel) const;

  static Poller* NewDefaultPoller(EventLoop* loop);

  void AssertInLoopThread() const { ownerLoop_->AssertInLoopThread(); }

 protected:
  typedef std::map<int, Channel*> ChannelMap;
  ChannelMap channels_;

 private:
  EventLoop* ownerLoop_;
};
}  // namespace fio66::net

#endif  // _SRC_NET_POLLER_H_
